package com.oa.cms.controller;

import com.oa.cms.controller.api.CmsConfigureApi;
import com.oa.cms.converter.CmsConfigureConverter;
import com.oa.cms.domain.dto.CmsConfigureDto;
import com.oa.cms.domain.entity.CmsConfigure;
import com.oa.cms.domain.enums.Status;
import com.oa.cms.query.CmsConfigureQuery;
import com.oa.cms.query.base.Page;
import com.oa.cms.service.CmsConfigureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gold-jx on 2017/12/7.
 */
@RestController
@Api(description = "CMS -> 配置管理")
public class CmsConfigureApiController implements CmsConfigureApi{
    @Autowired
    private CmsConfigureService cmsConfigureService;

    @Autowired
    private CmsConfigureConverter cmsConfigureConverter;



    @Override
    public ResponseEntity<Void> addCmsConfigure(@ApiParam(value = "CMS配置新增", required = true) @RequestBody CmsConfigureDto cmsConfigureDto) {
        Assert.notNull(cmsConfigureDto,"入参不能为空");
        CmsConfigure cmsConfigure=cmsConfigureConverter.convertToEntity(cmsConfigureDto);
        cmsConfigureService.save(cmsConfigure);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Void> updateCmsConfigure(@ApiParam(value = "CMS版本修改", required = true) @RequestBody CmsConfigureDto cmsConfigureDto) {

        CmsConfigure cmsConfigure= cmsConfigureConverter.convertToEntity(cmsConfigureDto);
        Assert.notNull(cmsConfigure.getId(),"修改ID不能为空");
        cmsConfigureService.update(cmsConfigure);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Void> deleteRealCmsConfigure(@ApiParam(value = "CMS版本真删除", required = true) @RequestBody String id) {
        Assert.notNull(id,"id不能为空");
        long id1;
        try {
            id1=Long.parseLong(id);
            cmsConfigureService.delete(id1);
        }catch (Exception e){
            Assert.isTrue(true,"id传入值异常");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

    }

    @Override
    public ResponseEntity<Void> deleteFalseCmsConfigure(@ApiParam(value = "CMS版本假删除", required = true) @RequestBody String id) {
        Assert.notNull(id,"id不能为空");
        long id1;
        try {
            id1=Long.parseLong(id);
            CmsConfigure cmsConfigure=new CmsConfigure();
            cmsConfigure.setId(id1);
            cmsConfigure.setStatus(Status.DELETED);
            cmsConfigureService.delete(id1);
        }catch (Exception e){
            Assert.isTrue(true,"id传入值异常");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Page<CmsConfigureDto>> getConfigure(CmsConfigureQuery cmsConfigureQuery) {
        Page<CmsConfigureDto> cmsConfigureDtoPage = cmsConfigureService.list(cmsConfigureQuery);
        if(!StringUtils.isEmpty(cmsConfigureDtoPage.getDatas())){
            List<CmsConfigureDto> cmsConfigureDtos=cmsConfigureDtoPage.getDatas().stream().map(request -> {
                CmsConfigureDto cmsConfigureDto=new CmsConfigureDto();
                cmsConfigureDto.setId(request.getId());
                cmsConfigureDto.setFatherId(request.getFatherId());
                cmsConfigureDto.setTopId(request.getTopId());
                cmsConfigureDto.setConfigType(request.getConfigType());
                cmsConfigureDto.setConfigName(request.getConfigName());
                cmsConfigureDto.setIconPath(request.getIconPath());
                cmsConfigureDto.setCreateTime(request.getCreateTime());
                cmsConfigureDto.setStatus(request.getStatus());
                cmsConfigureDto.setSort(request.getSort());
                return cmsConfigureDto;
            }).collect(Collectors.toList());
            return ResponseEntity.ok(new Page<>(cmsConfigureDtoPage.getTotalCount(), cmsConfigureDtos));
        }

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
